from d4.models import D4Dic
from d4.serializers import D4DicSerializer
from rest_framework.viewsets import ModelViewSet
# from django_filters import rest_framework as filters
from rest_framework import filters


class DicGVS(ModelViewSet):
    '''
    CBV下的系统字典管理API
    '''
    queryset = D4Dic.objects.all()
    serializer_class = D4DicSerializer

    # 配置过滤器
    # 也可以在类中注册过滤组件，可以代替或覆盖全局注册
    # filter_backends = [filters.DjangoFilterBackend]
    # filterset_fields = ['name', 'code']
    # search_fields = ['name', 'code']
    # ordering_fields = ['name', 'code']


class DicFilter(filters.FilterSet):
    """ 字典表 """
    s_id = filters.CharFilter(field_name='id', lookup_expr='icontains')
    s_fid = filters.CharFilter(field_name='fid', lookup_expr='icontains')
    s_name = filters.CharFilter(field_name='name', lookup_expr='icontains')
    s_value = filters.CharFilter(field_name='value', lookup_expr='icontains')
    s_code = filters.CharFilter(field_name='code', lookup_expr='icontains')
    s_description = filters.CharFilter(field_name='description', lookup_expr='icontains')
    s_is_locked = filters.CharFilter(field_name='is_locked', lookup_expr='icontains')
    s_is_delete = filters.CharFilter(field_name='is_delete', lookup_expr='icontains')
    s_sort = filters.NumberFilter(field_name='sort', lookup_expr='icontains')
    s_created_by = filters.CharFilter(field_name='created_by', lookup_expr='icontains')
    s_created_date = filters.DateTimeFilter(field_name='created_date', lookup_expr='icontains')
    s_update_by = filters.CharFilter(field_name='update_by', lookup_expr='icontains')
    s_update_date = filters.DateTimeFromToRangeFilter(field_name='update_date', lookup_expr='icontains')
    s_type = filters.CharFilter(field_name='type', lookup_expr='icontains')
    s_status = filters.CharFilter(field_name='status', lookup_expr='icontains')

    class Meta:
        # 模型类名
        model = D4Dic
        # 可以过滤的字段
        fields = ['name', 'code']
